﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Collections.TreeCollections;
using System.Xml.Linq;

namespace FeatureModelCore.Configuration
{
    public class ConfigNode : TreeNode
    {
        public readonly static string _XML_NODE_NAME = "NodeName";

        #region Properties
        public override string Name { get { return this._nodeName; } }
        public ConfigRelationship[] ChildrenRelationshipConfigures { get { return childrenGroupConfigure(); } }
        public ConfigNode[] ChildrenNodes { get { return childrenNodes(); } }
        public ConfigNode ParentNode { get { return ((ConfigRelationship)Parent).ParentNode; } }
        public ConfigRoot ParentRoot { get { return ((ConfigRelationship)Parent).ParentRoot; } }
        #endregion


        #region Fields
        private string _nodeName;

        #endregion

        public ConfigNode(TreeNodeIdentify id, string nodeName)
            : base(id)
        {
            _nodeName = nodeName;
        }


        /// <summary>
        /// args[0]: name 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static ConfigNode ConstructNode(
            TreeNodeIdentify id, object[] args)
        {
            string name = string.Empty;
            if (args != null)
            {
                name = args[0].ToString();
            }
            return new ConfigNode(id, name);
        }

        /// <summary>
        /// To String
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0},{1}"
                , base.ToString()
                , this._nodeName);
        }


        #region Xml Parser
        /// <summary>
        /// To Xelement
        /// </summary>
        /// <returns></returns>
        public override XElement ToXElem()
        {
            XElement xelem = base.ToXElem();
            xelem.Add(new XAttribute(_XML_NODE_NAME, _nodeName));

            return xelem;
        }
        /// <summary>
        /// Xml Parser
        /// </summary>
        /// <param name="xelem"></param>
        /// <returns></returns>
        internal static void xmlParser(XElement xelem, TreeObject obj)
        {
            ConfigNode node = obj as ConfigNode;
            if (node == null)
            {
                throw new Exception("Type Error!");
            }

            string nodeName = xelem.Attribute(_XML_NODE_NAME).Value;

            node._nodeName = nodeName;
        }
        #endregion

        #region Get Tree hierarchy Objects
        /// <summary>
        /// Get all children nodes
        /// </summary>
        /// <returns></returns>
        private ConfigNode[] childrenNodes()
        {
            List<ConfigNode> nodes = new List<ConfigNode>();
            foreach (ConfigRelationship r in ChildrenRelationshipConfigures)
            {
                nodes.AddRange(r.ChildrenNodes);
            }

            return nodes.ToArray<ConfigNode>();
        }
        /// <summary>
        /// Get all children group relationship
        /// </summary>
        /// <returns></returns>
        private ConfigRelationship[] childrenGroupConfigure()
        {
            List<ConfigRelationship> gRelationships
                = new List<ConfigRelationship>();
            foreach (INonRoot child in this.Children)
            {
                if (child is ConfigRelationship)
                {
                    gRelationships.Add((ConfigRelationship)child);
                }
            }
            return gRelationships.ToArray<ConfigRelationship>();
        }
        #endregion

        /// <summary>
        /// Parse to tree root
        /// </summary>
        /// <returns></returns>
        public override TreeRoot Parse2TreeRoot()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Clone
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            throw new NotImplementedException();
        }
    }
}
